/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class HelpActivity extends Activity {
	WebView webView;
	Button OKBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.help);
		webView = (WebView)findViewById(R.id.helpcontent);
		OKBtn = (Button)findViewById(R.id.ok_btn);
		OKBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		webView.loadDataWithBaseURL(null,getHelpDocFromResources(), "text/html", "utf-8",null);
		
	}
	
	private String getHelpDocFromResources() {
		InputStream is = null;
		try {
			is = getResources().openRawResource(R.raw.help);
			
			final char[] buffer = new char[0x10000];
			StringBuilder out = new StringBuilder();
			Reader in = new InputStreamReader(is, "UTF-8");
			int read;
			do {
			  read = in.read(buffer, 0, buffer.length);
			  if (read>0) {
			    out.append(buffer, 0, read);
			  }
			} while (read>=0);
			
			return out.toString();
		}
		catch (Exception e) {
			Log.e("getwit", e.toString());
		}
		finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					Log.e("getwit", e.toString());
				}
			}
		}
		
		return "";
	}
}
